//
//  PopoverAnimator.swift
//  DSWeibo
//
//  Created by liuzhu on 2018/11/10.
//  Copyright © 2018 liuzhu. All rights reserved.
//

import UIKit
let XMGPopoverAnimatorWillShow = "XMGPopoverAnimatorWillShow"
let XMGPopoverAnimatorWillDismiss = "XMGPopoverAnimatorWillDismiss"
class PopoverAnimator: NSObject,UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning{
    var isPresent:Bool = false
    var presentedFrame = CGRect.zero
    //实现代理方法，告诉系统谁来负责转场动画
    //UIPresentationController就是负责转场动画的控制器
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController?{
        let pc = PopoverPresentationController(presentedViewController: presented, presenting: presenting)
        pc.presentedFrame = presentedFrame
        return pc
    }
    
    /// //告诉系统谁来负责modal的展现
    ///
    /// - Parameters:
    ///   - presented: 被展现的视图
    ///   - presenting: 发起的视图
    ///   - source: <#source description#>
    /// - Returns: 谁来负责
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning?{
        isPresent = true
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: XMGPopoverAnimatorWillShow), object: self)
        return self
    }
    
    //告诉系统谁来负责modal的消失动画
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?{
        isPresent = false
         NotificationCenter.default.post(name: NSNotification.Name(rawValue: XMGPopoverAnimatorWillDismiss), object: self)
        return self
    }
    
    //动画时长
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.4
    }
    
    //如何动画
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        if isPresent {
            //展开
            let toView = transitionContext.view(forKey: UITransitionContextViewKey.to)
            toView?.transform = CGAffineTransform(scaleX: 1.0,y: 0.0)
            transitionContext.containerView.addSubview(toView!)
            toView?.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
            UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
                toView?.transform = CGAffineTransform.identity
            }) { (_) in
                transitionContext.completeTransition(true)
            }
        }else{
            //关闭
            let fromView = transitionContext.view(forKey: UITransitionContextViewKey.from)
            UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
                //CGFloat是不准确的，所以如果y写0.0会没有动画
                fromView?.transform = CGAffineTransform(scaleX: 1.0, y: 0.00000000001)
            }) { (_) in
                transitionContext.completeTransition(true)
            }
        }
    }
}

